package com.course.dao;

import java.util.List;

import com.course.domain.College;
import com.course.domain.Department;

public class DepartmentDao extends SupperDao
{

	@SuppressWarnings( "unchecked" )
	public long countOfDepartment( int collegeId )
	{
		String hql = "select count(*) from Department where college.id = " + collegeId;
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Department > getDepartments( int start, int length, int collegeId )
	{
		String hql = "from Department where college.id = " + collegeId;
		return this.findPaging( hql, start, length, null );
	}

	public Department getDepartmentById( int departmentId )
	{
		return ( Department )this.get( Department.class, departmentId );
	}

	public boolean saveDepartment( String name, Integer collegeId )
	{
		Department department = new Department();
		department.setName( name );
		department.setCollege( ( College )this.get( College.class, collegeId ) );
		return this.save( department );
	}

	public boolean updateDepartment( Integer id, String name, Integer collegeId )
	{
		Department department = ( Department )this.get( Department.class, id );
		department.setName( name );
		department.setCollege( ( College )this.get( College.class, collegeId ) );
		return this.update( department );
	}

	public boolean deleteDepartmentById( Integer id )
	{
		Department department = ( Department )this.get( Department.class, id );
		return this.delete( department );
	}

	@SuppressWarnings( "unchecked" )
	public List< Department > getDepartmentsByCollegeId( int collegeId )
	{
		String hql = "from Department d where d.college.id = " + collegeId;
		return this.findByHQL( hql );
	}

	@SuppressWarnings( "unchecked" )
	public Department getDepartmentBySpecialtyId( Integer specialtyId )
	{
		List list = null;
		String hql = "select d from Department d, Specialty s where s.department.id = d.id and s.id = " + specialtyId;
		list = ( List )this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		return ( Department )list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public Department getDepartmentByTeacherId( Integer teacherId )
	{
		List list = null;
		String hql = "select d from Department d, Teacher t where t.department.id = d.id and t.id = " + teacherId;
		list = ( List )this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		return ( Department )list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public long countOfDepartmentByDepartmentName( String name )
	{
		String hql = "select count(*) from Department d where d.name = '" + name + "'";
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Department > getDepartmentsByDepartmentName( int start, int length, String name )
	{
		String hql = "from Department d where d.name = '" + name + "'";
		return this.findPaging( hql, start, length, null );
	}

	@SuppressWarnings( "unchecked" )
	public long countOfAllDepartment()
	{
		String hql = "select count(*) from Department";
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Department > getAllDepartment( int start, int length )
	{
		String hql = "from Department";
		return this.findPaging( hql, start, length, null );
	}

}
